package com.woman.beautylive.presentation.ui.main;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.woman.beautylive.BeautyLiveApplication;
import com.woman.beautylive.R;
import com.woman.beautylive.domain.LocalDataManager;
import com.woman.beautylive.presentation.ui.base.BaseFragment;
import com.woman.beautylive.presentation.ui.chatting.ConversationListFragment;
import com.woman.beautylive.presentation.ui.chatting.MainView;
import com.woman.beautylive.presentation.ui.chatting.ViewPagerAdapter;
import com.woman.beautylive.util.L;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;

/**
 * Chatting入口Activity, 可以选择单聊或群聊,并且设置聊天相关的用户信息(通过Intent的方式)
 */

public class MessageFragment extends BaseFragment {

    private Dialog mDialog;
    private String mMyName;
    private String mMyPassword;
    private static final int REGISTER = 200;
    private Context mContext;
    private boolean r=false;
    private boolean f=false;

    private MainController mMainController;
    private MainView mMainView;
    @Override
    protected int getLayoutId() {
        return R.layout.jmui_activity_main;
    }

    @Override
    protected void initViews(View view) {
        mMainView = (MainView) view.findViewById(R.id.main_view);
        mMainView.initModule();
        mMainController = new MainController(mMainView);

        mMainView.setOnClickListener(mMainController);
        mMainView.setOnPageChangeListener(mMainController);

        mContext = getActivity();
        //注册接收消息(成为订阅者), 注册后可以直接重写onEvent方法接收消息
        JMessageClient.registerEventReceiver(this);
        LinearLayout mSingleChatLl;
//        LinearLayout mGroupChatLl;
        mSingleChatLl = (LinearLayout) view.findViewById(R.id.jmui_single_chat_ll);
//        mGroupChatLl = (LinearLayout) findViewById(R.id.jmui_group_chat_ll);

        mMyPassword = "meilibo"+ LocalDataManager.getInstance().getLoginInfo().getUserId();
        if (JMessageClient.getMyInfo() == null) {
            mMyName ="meilibo"+LocalDataManager.getInstance().getLoginInfo().getUserId();

            for (int i = 0; i < 5; i++) {
                L.e("lw", "注册操作" + i);
                register();
                if (r = true) {
                    break;
                }
            }
            if (r=true){
                for (int j = 0; j < 5; j++) {
                    L.e("lw", "登录操作"+j);
                    login();
                    if (f=true) {
                        break;
                    }
                }
            }
        }
    }

    public void login() {
        JMessageClient.login(mMyName, mMyPassword, new BasicCallback() {
            @Override
            public void gotResult(int status, String desc) {
                if (status == 0) {
                    L.e("lw","登录成功");
                    f=true;
                } else {
                    L.e("lw","登录失败"+status+desc);
                }
            }
        });
    }

    public void register() {
        JMessageClient.register(mMyName, mMyPassword, new BasicCallback() {
            @Override
            public void gotResult(int status, String desc) {
                if (status == 0) {
                    L.e("lw","注册成功");
                    r=true;
                } else {
                    L.e("lw", "注册失败" + status + desc);
                    if (desc.equals("user exist")) {
                        r=true;
                    }
                }
            }
        });
    }

    public static MessageFragment newInstance() {
        return new MessageFragment();
    }

    public class MainController implements View.OnClickListener, ViewPager.OnPageChangeListener {

        private final static String TAG = "MainController";

        private ConversationListFragment mConvListFragment;
        private MainView mMainView;
        private ProgressDialog mDialog;
        // 裁剪后图片的宽(X)和高(Y), 720 X 720的正方形。
        private  int OUTPUT_X = 720;
        private  int OUTPUT_Y = 720;

        public MainController(MainView mMainView) {
            this.mMainView = mMainView;
            setViewPager();
        }

        private void setViewPager() {
            List<Fragment> fragments = new ArrayList<Fragment>();
            // init Fragment
            mConvListFragment = new ConversationListFragment();
            fragments.add(mConvListFragment);
            ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager(),
                    fragments);
            mMainView.setViewPagerAdapter(viewPagerAdapter);
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }

        /**
         * 裁剪图片
         */
        public void cropRawPhoto(Uri uri) {

            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");

            // 设置裁剪
            intent.putExtra("crop", "true");

            // aspectX , aspectY :宽高的比例
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            // outputX , outputY : 裁剪图片宽高
            intent.putExtra("outputX", OUTPUT_X);
            intent.putExtra("outputY", OUTPUT_Y);
            intent.putExtra("return-data", false);
            intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent, BeautyLiveApplication.REQUEST_CODE_CROP_PICTURE);
        }

        @Override
        public void onPageSelected(int index) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        public void sortConvList() {
            mConvListFragment.sortConvList();
        }

    }
}